package org.darcy.controller.sys;

import java.util.List;

import org.darcy.entity.sys.SysFile;
import org.darcy.framework.model.R;
import org.darcy.service.sys.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "文件上传接口")
@RestController
@RequestMapping("/upload")
public class UploadController {

	@Autowired
	private UploadService uploadService;

	@PostMapping(consumes = "multipart/form-data")
	//@Operation(summary = "单个文件上传")
	public R upload(@RequestParam(required = false) String type, @RequestPart("file") MultipartFile multipartFile) {
		if (StrUtil.isEmpty(type)) {
			type = "any";
		}
		SysFile file = uploadService.upload(type, multipartFile);
		return R.ok().data(file);
	}

	@PostMapping(value = "/batch", consumes = "multipart/form-data")
	@Operation(summary = "多个文件上传")
	public R uploadBatch(@RequestParam(required = false) String type,
			@RequestPart("files") List<MultipartFile> multipartFiles) throws Exception {
		List<SysFile> list = uploadService.uploadBatch(type, multipartFiles);
		return R.ok().data(list);
	}

}
